package com.sy.server.controller;

import com.sy.server.model.Feedback;
import com.sy.server.privilege.anno.Permission;
import com.sy.server.service.IFeedbackService;
import com.sy.server.service.impl.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping(value = "/feedback")
@Permission
public class FeedbackController {
    @Autowired
    private IFeedbackService service;

    @Permission
    @GetMapping(value = "/queryAll")
    public ResponseEntity<List<Feedback>> queryAll(){
        List<Feedback> list=service.queryAll();
        return new ResponseEntity<>(list, HttpStatus.OK);
    }

    @Permission
    @GetMapping(value = "/praise/{fid}")
    public ResponseEntity<Boolean> praise(@PathVariable String fid){
        boolean praiseBefore=service.praise(fid);
        return new ResponseEntity<>(praiseBefore,HttpStatus.OK);
    }

    @Permission
    @PostMapping(value = "/add")
    public ResponseEntity<Void> add(@RequestBody Feedback feedback){
        service.add(feedback);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping(value = "/checkIfPraised/{fid}")
    public ResponseEntity<Boolean> checkIfPraised(@PathVariable String fid){
        boolean hasPraised=service.checkIfPraised(fid);
        return new ResponseEntity<>(hasPraised,HttpStatus.OK);
    }

    @GetMapping(value = "/checkIfComment/{fid}")
    public ResponseEntity<Boolean> checkIfComment(@PathVariable String fid){
        boolean hasCommented=service.checkIfComment(fid);
        return new ResponseEntity<>(hasCommented,HttpStatus.OK);
    }

    @DeleteMapping(value = "/deleteFeedback/{fid}")
    public ResponseEntity<Void> deleteFeedback(@PathVariable String fid){
        service.del(fid);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 已废弃，改为使用定时任务
     * @return
     */
    @Deprecated
    @PutMapping(value = "/syncLikes")
    public ResponseEntity<Void> syncLikes(){
        service.syncLikes();
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 获取点赞数，已废弃，在queryAll中有实现
     * @param fid
     * @return
     */
    @Deprecated
    @GetMapping(value = "/praiseCount/{fid}")
    public ResponseEntity<Long> praiseCount(@PathVariable String fid){
        Long count=service.praiseCount(fid);
        return new ResponseEntity<>(count,HttpStatus.OK);
    }
}
